package cn.enzo.utils.utils.GenerateFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

import cn.enzo.utils.utils.SqlMapper;
import freemarker.template.Configuration;
import freemarker.template.Template;

public class AbstractGenerateFile<T> implements GenerateFile<T> {
	String basepath = System.getProperty("user.dir") + File.separator + "src" + File.separator + "main" + File.separator
			+ "java" + File.separator;

	private static final String DAO_PACKAGE = "daoback";

	private static final String MAPPER_PACKAGE = "mapperback";

	private static final String SERVICE_PACKAGE = "serviceback";

	private static final String SERVICEIMPL_PACKAGE = "serviceimpl";

	@Override
	public void createDao(T t) {

		Map<String, String> map = new HashMap<String, String>();
		String filename = t.getClass().getSimpleName() + "Dao";
		map.put("filename", filename);
		map.put("name", t.getClass().getName());
		map.put("simplename", t.getClass().getSimpleName());
		String classPath = getClassPath(t.getClass());

		String dir = getTargetPath(classPath, DAO_PACKAGE);
		File file2 = new File(dir, filename + ".java");

		String replace = getPackAgeBypath(dir);
		map.put("package", replace);
		createnewfile(file2.getAbsolutePath(), "dao.ftl", map);
	}

	@Override
	public void createMapper(T t) {
		System.out.println("createMapper");

		try {

			SqlMapper sqlMapper = new SqlMapper();
			Map<String, String> hashMap = new HashMap<String, String>();
			String tableName = "sale";
			String filename = t.getClass().getSimpleName() + "Mapper";
			hashMap.put("save", sqlMapper.getsaveMapper(t.getClass(), tableName));

			hashMap.put("update", sqlMapper.getUpdateMapper(t.getClass(), tableName));
			hashMap.put("deleteBykey", sqlMapper.getDeleteMapper(t.getClass(), tableName));

			String classPath = getClassPath(t.getClass());

			String targetPath = getTargetPath(classPath, MAPPER_PACKAGE);
			String DaotargetPath = getTargetPath(classPath, DAO_PACKAGE);
			hashMap.put("namespace",
					getPackAgeBypath(DaotargetPath + File.separator + t.getClass().getSimpleName() + "Dao"));

			File file2 = new File(targetPath, filename + ".xml");

			createnewfile(file2.getAbsolutePath(), "mapper.ftl", hashMap);

		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void createService(T t) {
		System.out.println("createService");

		Map<String, String> map = new HashMap<String, String>();
		String filename = "I" + t.getClass().getSimpleName() + "Service";
		map.put("filename", filename);
		map.put("name", t.getClass().getName());
		map.put("simplename", t.getClass().getSimpleName());
		String classPath = getClassPath(t.getClass());
		String dir = getTargetPath(classPath, SERVICE_PACKAGE);
		File file2 = new File(dir, filename + ".java");

		String replace = getPackAgeBypath(dir);
		map.put("package", replace);
		createnewfile(file2.getAbsolutePath(), "service.ftl", map);
	}

	@Override
	public void createServiceImpl(T t) {
		System.out.println("createServiceImpl");
		String classPath = getClassPath(t.getClass());

		Map<String, String> map = new HashMap<String, String>();
		String filename = t.getClass().getSimpleName() + "ServiceImpl";
		String dir = getTargetPath(classPath, SERVICEIMPL_PACKAGE);
		String replace = getPackAgeBypath(dir);
		File file2 = new File(dir, filename + ".java");
		
		String DaotargetPath = getTargetPath(classPath, DAO_PACKAGE);
		String servicenametargetPath = getTargetPath(classPath, SERVICE_PACKAGE);
		
		map.put("daopackage", getPackAgeBypath(DaotargetPath + File.separator + t.getClass().getSimpleName() + "Dao"));
		map.put("servicepackage", getPackAgeBypath(servicenametargetPath + File.separator +"I"+ t.getClass().getSimpleName() + "Service"));
		map.put("servicename", "I"+ t.getClass().getSimpleName() + "Service");
		map.put("daoname", t.getClass().getSimpleName() + "Dao");
		map.put("filename", filename);
		map.put("name", t.getClass().getName());
		map.put("simplename", t.getClass().getSimpleName());
		map.put("package", replace);
		createnewfile(file2.getAbsolutePath(), "serviceimpl.ftl", map);

	}

	@Override
	public void createController(T t) {
		System.out.println("createController");

	}

	public void createnewfile(String filename, String tempname, Map<?, ?> map) {
		try {
			String path = System.getProperty("user.dir");
			String dir = File.separator + "src" + File.separator + "main" + File.separator + "webapp" + File.separator
					+ "temple" + File.separator;
			Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
			cfg.setDirectoryForTemplateLoading(new File(path + dir));
			Template template = cfg.getTemplate(tempname);
			String targerfile = filename;
			System.out.println(targerfile);
			template.process(map, new OutputStreamWriter(new FileOutputStream(targerfile), "UTF-8"));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 通过类的.java 文件，得到文件的绝对路径
	 * 
	 * @param t
	 * @return
	 */
	private String getClassPath(Class<? extends Object> class1) {
		String name = class1.getPackage().getName();
		String replace = name.replace(".", File.separator);

		return basepath + replace;
	}

	/**
	 * 根据当前类得到类型的目录
	 * 
	 * @param path
	 * @param type
	 * @return
	 */
	private String getTargetPath(String path, String type) {
		String parent = new File(path).getParentFile().getAbsolutePath();
		File file = new File(parent + File.separator + type);
		if (!file.exists()) {
			file.mkdirs();
		}
		return file.getPath();
	}

	/**
	 * 
	 */
	private String getPackAgeBypath(String path) {
		return path.replace(basepath, "").replace(File.separator, ".");
	}

	@Override
	public void createJSP(T t) {
		// TODO Auto-generated method stub
	}
}
